package com.linkage.seapeak.activity.vote;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.adapter.Vote.ShowVoteMemberGroupAdapter;
import com.linkage.seapeak.base.BaseWhiteActivity;
import com.linkage.seapeak.net.ResultCallBack;
import com.linkage.seapeak.net.VoteNetRequest;
import com.linkage.seapeak.pojo.CrediterUser;
import com.linkage.seapeak.pojo.ParticipantsPageList;
import com.linkage.seapeak.pojo.VoteGroup;
import com.seapeak.recyclebundle.AYSwipeRecyclerView;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示表决成员 有分组
 */
public class ShowVoteMemberGroupActivity extends BaseWhiteActivity implements AYSwipeRecyclerView.OnRefreshLoadListener {

    private String projectId;
    private String voteId;

    private AYSwipeRecyclerView mRecyclerView;
    private ShowVoteMemberGroupAdapter mDataAdapter;

    private int voteRule;

    private List<VoteGroup> mData;
    private List<Object> setData;

    private VoteGroup currentVoteGroup = null;

    int count = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_voters_group, "表决人员");
        projectId = getIntent().getStringExtra("projectId");
        voteId = getIntent().getStringExtra("voteId");
        voteRule = getIntent().getIntExtra("voteRule", -1);
        init();
        mRecyclerView.startLoadFirst();
    }

    public void init() {
        mRecyclerView = findViewById(R.id.list);

        mData = new ArrayList<>();
        setData = new ArrayList<>();
        mDataAdapter = new ShowVoteMemberGroupAdapter(this, setData);
        mRecyclerView.setMode(AYSwipeRecyclerView.SwipeType.ONLY_START);
        mRecyclerView.setShowEmpty(false);
        mRecyclerView.setOnRefreshLoadLister(this);
        mRecyclerView.setAdapter(mDataAdapter);

        mDataAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position, RecyclerView.ViewHolder viewHolder) {
                VoteGroup item;
                switch (v.getId()) {
                    case R.id.item_group_root:
                        item = (VoteGroup) setData.get(position);
                        if (currentVoteGroup != null && item.getId().equals(currentVoteGroup.getId())) {
                            item.isExpand = !item.isExpand;
                            currentVoteGroup = null;
                            notifyDataChanged();
                        } else {
                            currentVoteGroup = item;
                            getEditVoteMember(currentVoteGroup);
                        }
                        break;
                }
                notifyDataChanged();
            }
        });
    }

    /**
     * 重新计算，刷新
     */
    public void notifyDataChanged() {
        setData.clear();
        count = 0;
        if (mData != null && mData.size() > 0) {
            for (VoteGroup group : mData) {
                setData.add(group);
                if (currentVoteGroup != null) {
                    if (group.getId().equals(currentVoteGroup.getId()))
                        group.isExpand = true;
                    else
                        group.isExpand = false;
                }
                List<CrediterUser> members = group.getMembers();
                if (members != null && members.size() > 0 && group.isExpand) {
                    for (CrediterUser user : members)
                        setData.add(user);
                }
                count += group.getMemberCount();
            }
        }
//        setTitle(count == 0 ? "表决人员" : "表决人员(" + count + ")");
        mRecyclerView.onFinishRequest(false, false);
    }

    /**
     * 获取列表
     */
    private void getEditVoteMember(final VoteGroup group) {
        VoteNetRequest.showVoteMemberTable(getUserToken(), voteId, group.getId(),
                projectId, "", new ResultCallBack<List<CrediterUser>>() {
                    @Override
                    public void onSuccess(List<CrediterUser> result) {
                        if (group.getMembers() != null) {
                            group.getMembers().clear();
                            group.getMembers().addAll(result);
                        } else
                            group.setMembers(result);

                        group.setMemberCount(result.size());
//                如果全部获取成功了就刷新列表页面
                        notifyDataChanged();
                    }

                    @Override
                    public void onFailed(String msg) {
                        notifyDataChanged();
                    }
                });
    }

    /**
     * 获取群组列表
     */
    private void getEditGroupList(VoteGroup group) {
        VoteNetRequest.showVoteGroupMemberTable(getUserToken(), voteId,
                projectId, voteRule == -1 ? "" : voteRule + "", new ResultCallBack<List<VoteGroup>>() {
                    @Override
                    public void onSuccess(List<VoteGroup> result) {
                        mData.clear();
                        mData.addAll(result);
                        int totalcount = 0;
                        for (int i = 0; i < mData.size(); i++) {
                            if (group != null) {
                                if (group.getId().equals(mData.get(i).getId())) {
                                    currentVoteGroup = mData.get(i);
                                    mData.get(i).isExpand = true;
                                    getEditVoteMember(mData.get(i));

                                } else {
                                    mData.get(i).isExpand = false;
                                }


                            } else {
                                if (i == 0) {
                                    currentVoteGroup = mData.get(i);
                                    mData.get(i).isExpand = true;
                                    getEditVoteMember(mData.get(i));
                                } else {
                                    mData.get(i).isExpand = false;
                                }


                            }
                            if(mData.get(i).getParticipantsPageList()!=null) {
                                totalcount += mData.get(i).getParticipantsPageList().getTotalCount();
                            }
                        }
                        setTitle( "表决人员(" + totalcount + ")");
                        notifyDataChanged();
                    }

                    @Override
                    public void onFailed(String msg) {
                        notifyDataChanged();
                    }
                });
    }

    @Override
    public void loadFirst() {
        getEditGroupList(null);
    }

    @Override
    public void loadNext() {

    }

}
